package com.itwk.gulimall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itwk.common.to.MemberPrice;
import com.itwk.common.to.SkuReductionTo;
import com.itwk.common.utils.PageUtils;
import com.itwk.common.utils.Query;
import com.itwk.gulimall.coupon.dao.SmsMemberPriceDao;
import com.itwk.gulimall.coupon.entity.SmsMemberPriceEntity;
import com.itwk.gulimall.coupon.service.SmsMemberPriceService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("smsMemberPriceService")
public class SmsMemberPriceServiceImpl extends ServiceImpl<SmsMemberPriceDao, SmsMemberPriceEntity> implements SmsMemberPriceService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SmsMemberPriceEntity> page = this.page(
                new Query<SmsMemberPriceEntity>().getPage(params),
                new QueryWrapper<SmsMemberPriceEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuMemberPrice(SkuReductionTo skuReductionTo) {
        //商品微服务发布商品远程调用保存会员等级对应的价格信息
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        List<SmsMemberPriceEntity> collect = memberPrice.stream().filter(price->{
            //取出有折扣信息的数据，没有则不保存
            return price .getPrice().compareTo(new BigDecimal(0))==1;
        }).map(member -> {
            SmsMemberPriceEntity smsMemberPriceEntity = new SmsMemberPriceEntity();
            smsMemberPriceEntity.setSkuId(skuReductionTo.getSkuId());
            smsMemberPriceEntity.setMemberLevelId(member.getId());
            smsMemberPriceEntity.setMemberLevelName(member.getName());
            smsMemberPriceEntity.setMemberPrice(member.getPrice());
            return smsMemberPriceEntity;
        }).collect(Collectors.toList());

        this.saveBatch(collect);
    }

}